$(function(){
    if(window.parent == window.self){
        max_width = window.document.documentElement.clientWidth;
        max_height = window.document.documentElement.clientHeight;
    }else{
        var panel = $(".tabs-panels",window.parent.document);

        max_width = panel.width;
        max_height = panel.height;
    }

    $('#listmovement').datagrid({
        toolbar: '#toolbar',
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        fit:true,
        collapsible:false,
        url: '/storage/movement/list',
        queryParams:{
            _token : $('meta[name="csrf-token"]').attr('content'),
            curr_menu_id : $('meta[name="curr_menu_id"]').attr('content')
        },
        columns: [[
            { field:'id',checkbox:true},
            { title: '调拨单号', field: 'sn',width: 130 },
            { title: '调拨类型', field: 'doctype_id', width: 130 },
            { title: '调拨日期', field: 'movementdate', width: 130 },
            { title: '调出仓库ID', field: 'warehouse_id', width: 130 },
            { title: '调入仓库ID', field: 'warehouseto_id', width: 130 },
            { title: '调拨状态', field: 'doc_state', width: 130 },
            { title: '描述', field: 'description', width: 130 }
        ]],
        onClickRow:function (index,raw) {
            $("input[name='raw_id']").val(raw.id);
            $('#goodslist').datagrid({
                url: 'storage/movement/goodslist?movement_id='+raw.id,
                pagination: true,
                rownumbers: true,
                fit: true,
                fitColumns:true,
                singleSelect:"true",
                queryParams:{
                    _token : $('meta[name="csrf-token"]').attr('content'),
                },
                columns:[[
                    { field:'id',checkbox:true},
                    { field:'goods_id',title:'商品ID',width:80,align:'center'},
                    { field:'goods_name',title:'商品名称',width:100,align:'center'},
                    { field:'movementqty',title:'调拨数量',width:100,align:'center'}
                ]]
            });
        }
    });
});

function openAdd(){
    $('#form_dialog').dialog({
        href: '/storage/movement/add',
    });
}

function openEdit(){
    var rows = $('#listwarehouse').datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("提示信息", "请选择需要编辑的行" , "info");
        return;
    }
    $('#form_dialog').dialog({
        title: '编辑信息',
        width: 350,
        height: 350,
        closed: false,
        cache: false,
        href: '/storage/warehouse/edit?warehouse_id='+rows.id,
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function save(){
    var params = $('#info-form').serialize();
    var province = $('#province').val();
    var city = $('#city').val();
    var area = $('#district').val();
    var area_info = $('#province option:selected').text()+' '+$('#city option:selected').text()+' '+$('#district option:selected').text();
    params += '&city='+city+'&province='+province+'&area='+area+'&area_info='+area_info;
    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url:'/storage/warehouse/save',
        type:'post',
        data:params,
        dataType:'json',
        success:function (data) {
            if(data.code == 200){
                $('#listwarehouse').datagrid("reload")
                $('#form_dialog').dialog('close');
            }else{
                $.messager.alert("提示信息", data.data , "error");
            }
        }
    });
}

function reload(){
    $('#listmovement').datagrid("reload");
}
